﻿
@{
    ViewBag.Title = "GetAllSalesLeadHistory";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>SalesLeadHistory</h2>

<div id="GetAllSalesLeadGrid"></div>


<script type="text/javascript">
    var leadContactId = 0;
 
        var gridInEditMode = false;
       
        $('#GetAllSalesLeadGrid').show();
       
        $('#GetAllSalesLeadGrid').empty();
        var SalesGridDataSource = new kendo.data.DataSource({

            transport: {
                read: function (options) {

                    $.ajax({
                        type: "POST",
                        url: "../SalesLead/GetSalesLeadByCreateUser",
                        contentType: "application/json; charset=utf-8",
                        dataType: 'json',
                        cache: false,
                        data: JSON.stringify({ createdUser: "@User.Identity.Name" }),
                        success: function (data) {
                            debugger;
                            console.log(data);
                            options.success(data);
                        },

                        error: function (request, status, error) {
                            debugger;
                            alert(request.responseText);
                        }
                    });
                }
            },
            schema: {
            model: {
            id: "id",
            fields: {
            id: {
            editable: false, nullable: true,
        type: "int"
        },
        loggedOwner:
            {
                defaultValue: "@User.Identity.Name",
                type: "string"
            },
        name: {
                type: "string"
        },
        leadcontactname:
            {
                type: "string"
            },
        companycontactname:
            {
                type: "string"
            },
        email:
            {
                type: "string"
            },
        monthlyDollar:
            {
                type:"decimal"
            },
        setupDollar:
            {
                type:"decimal"
            },

        companyId: {
                type: "int"
               // defaultValue: companyId
        },
        phone: {
                type: "string"
        },
        product: {
                type: "string"

        },
        daysOpened:
            {
                type: "int"

            },
        opportunity: {
                type: "string"
            // validation: { required: true }
        },
        jobtitle: {
                type: "string"
        },
        notes:
            {
                type: "string"
            },
        numberOfEmployees: {
                type: "int"

        },
        annualRevenue: {
                type: "decimal"

        },
        deposit: {
                editable: true,
                type: "int"
        },

        estimatedCost: {
                type: "int",

                },
        actualCost: {
                type: "int",
            //  validation: { required: true }
                },
        dateOpened: {
                type: "date",
            //  validation: { required: true },
                nullable: true,
                value: "",
                defaultOption: null
        },
        dateClosed: {
                type: "date",
            //  validation: { required: true },
                nullable: true,
                value: "",
                defaultOption: null
        },
        code:
            {
                type: "string"
            },


        contractSentDate: {
                type: "date",
            //  validation: { required: true },
                nullable: true,
                value: "",
                defaultOption: null
        },
        contractSignedDate: {
                type: "date",
            //  validation: { required: true },
                nullable: true,
                value: "",
                defaultOption: null
        },

        depositReceivedDate:
            {
                type: "date",
                //  validation: { required: true },
                nullable: true,
                value: "",
                defaultOption: null
            },

        startDate: {
                type: "date",
            //  validation: { required: true },
                nullable: true,
                value: "",
                defaultOption: null
        },


        error: function (a) {
            $('#GetAllSalesLeadGrid').data("kendoGrid").cancelChanges();
        },
        }
        }
        }
            });

        var jobdataSource = new kendo.data.DataSource({
            transport: {
                read: function (options) {
                    $.ajax({
                        type: "GET",
                        url: "../ContactDetail/GetAllJobTitles",
                        contentType: "application/json; charset=utf-8",
                        dataType: 'json',
                        data: JSON.stringify({ key: "Value" }),
                        success: function (data) {
                            options.success(data);
                        }
                    });
                }
            }
        });
        var productsdataSource = new kendo.data.DataSource({
            transport: {
                read: function (options) {
                    $.ajax({
                        type: "GET",
                        url: "../ProductRequestGrid/GetProducts",
                        contentType: "application/json; charset=utf-8",
                        dataType: 'json',
                        data: JSON.stringify({ key: "value" }),
                        success: function (data) {
                            options.success(data);
                        }
                    });
                }
            }
        });


        var companycontactsdatasource = new kendo.data.DataSource({
            transport: {
                read: function (options) {
                    $.ajax({
                        type: "POST",

                        url: "../CommunicationMessageGrid/GetAllCompanyContacts",
                        contentType: "application/json; charset=utf-8",
                        dataType: 'json',
                        data: JSON.stringify({ id: companyId }),
                        success: function (data) {
                            options.success(data);
                        }
                    });
                }
            }
        });
        var allcontactsdatasource = new kendo.data.DataSource({
            transport: {
                read: function (options) {
                    $.ajax({
                        type: "GET",
                        url: "../CommunicationMessageGrid/GetAllContacts",
                        contentType: "application/json; charset=utf-8",
                        dataType: 'json',
                        success: function (data) {
                            options.success(data);
                        }
                    });
                }
            }
        });


        function dateTimeEditor(container, options) {
            $('<input data-text-field="' + options.field + '" data-value-field="' + options.field + '" data-bind="value:' + options.field + '" data-format="' + options.format + '"/>')
            .appendTo(container)
            .kendoDateTimePicker({});
        }
        $("#GetAllSalesLeadGrid").kendoGrid({
            dataSource: SalesGridDataSource,
            scrollable: true,
            height: "300px",
            toolbar: ["excel"], //, "save", "cancel"],
            excel: {
                allPages: true,
                fileName: "SalesProjectExport.xlsx"
            },
           
            scrollable: true,
            height: 250,
            columns: [
            { field: "loggedOwner", title: "Created User", filterable: true,width:"10em", editable: true },
           { field: "leadcontactname", title: "Lead Name ", filterable: false, width: "10em", editor: allcontactsAutoCompleteEditor, editable: true },
            { field: "ccontactname", title: "Company Conatact", editor: companycontactnameDropDownEditor, filterable: true, width: "12em" },
             { field: "companyname", title: "Company", filterable: true, width: "12em" },
            { field: "product", title: "Product", filterable: true, editor: productsDropDownEditor, width: "8em" },
            { field: "email", title: "Email", filterable: false, width: "8em" },
             { field: "phone", title: "Phone", filterable: true, width: "8em" },

           { field: "numberOfEmployees", title: "Number Of Employee", width: "12em" },
             { field: "annualRevenue", title: "Annual Revenue", format: "{0:c}", width: "12em" },
               { field: "monthlyDollar", title: "Monthly Dollar", format: "{0:c}", width: "12em" },
                 { field: "setupDollar", title: "Set Up Dollar", format: "{0:c}", width: "12em" },
               { field: "deposit", title: "Deposit", format: "{0:c}", width: "10em" },
                 { field: "code", title: "Job Title", editor: jobTitleDropDownEditor, width: "8em" },
                 { field: "opportunity", title: "Opportunity", editor: oportunityDropdownEditor, width: "10em" },

          { field: "estimatedCost", title: "Estimated Cost", format: "{0:c}", width: "10em" },
             { field: "actualCost", title: "Actual Cost", format: "{0:c}", width: "10em" },
              {
                  field: "dateOpened", title: "Date Opened", width: "10em", filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                  editor: dateTimeEditor,
                  filterable: {
                      ui: "datetimepicker"
                  }
              },
                 {
                     field: "dateClosed", title: "Date Closed", width: "10em", filterable: false, hidden:true, format: "{0:MM/dd/yyyy HH:mm tt}",
                     editor: dateTimeEditor,
                     filterable: {
                         ui: "datetimepicker"
                     }
                 },
                   { field: "daysOpened", title: "HowManyDaysOpened", filterable: true, width: "12em" },
                    {
                        field: "contractSentDate", title: "Contract Sent",  filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                        editor: dateTimeEditor,
                        width: "10em",
                        filterable: {
                            ui: "datetimepicker"
                        }
                    },
                      {
                          field: "contractSignedDate", title: "Contract Signed", hidden: true, filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                          editor: dateTimeEditor,
                          filterable: {
                              ui: "datetimepicker"
                          }
                      },
                         {
                             field: "depositReceivedDate", title: "Deposit Received", hidden: false, filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                             editor: dateTimeEditor,
                             filterable: {
                                 ui: "datetimepicker"
                             }
                         },
                            {
                                field: "startDate", title: "Start Date", filterable: false,hidden: false, editable: true, format: "{0:MM/dd/yyyy HH:mm tt}",
                                editor: dateTimeEditor, width: "8em",
                                filterable: {
                                    ui: "datetimepicker"
                                }
                            },

                 { field: "notes", title: "Notes", width: "10em" },


          {
              title: "&nbsp;",
              width: "10em",

            
          }
            ],
            filterable: {
                extra: false,
                operators: {
                    string: {
                        eq: "Is equal to",
                        neq: "Is not equal to",
                        contains: "Contains"
                    }
                }
            },

            editable: "popup",

            sortable: {
                mode: "single",
                allowUnsort: false
            },
            selectable: true,
            edit: function () {
                //   e.container.find(".k-edit-label:nth-child(3)").hide();
                gridInEditMode = true;
            },
            excelExport: function (e) {
                for (var i = 0; i < e.workbook.sheets[0].columns.length; i++) {
                    e.workbook.sheets[0].columns[i].autoWidth = true;
                }
            },
            cancel: function () {
                gridInEditMode = false;
            },

        });
        GetAllSalesLeadGrid.bind("requestEnd", showSuccessMessage);
        $(".close").click(function () {
            $(this).parent().removeClass('in');
        });
        function showSuccessMessage(e) {
            if (e.type === "update" || e.type === "create") {
                gridInEditMode = false;
                $("#saveConfirm").addClass('in');
                e.sender.read();
            }
        }

        function jobTitleDropDownEditor(container, options) {
            $('<input  id="jobtitleId" data-text-field="code" data-value-field="id" data-bind="value:titleId"/>').appendTo(container).kendoDropDownList({
                autoBind: true,
                dataValueField: "id",
                dataTextField: "code",
                dataSource: jobdataSource
            });
        }
        function productsDropDownEditor(container, options) {

            $('<input id="ProductValue" data-text-field="product" data-value-field="id"  data-bind="value:productId"/>').appendTo(container).kendoDropDownList({
                autoBind: true,
                dataValueField: "id",
                optionLabel: "select a product",
                dataTextField: "product",
                dataSource: productsdataSource
            });
            $('<span class="k-invalid-msg" data-for="product"></span>').appendTo(container);

        }


        function companycontactnameDropDownEditor(container, options) {

            $('<input id="companyctId" data-text-field="name" data-value-field="ctId"  data-bind="value:contactId" />').appendTo(container).kendoDropDownList({
                autoBind: true,
                dataValueField: "ctId",
                dataTextField: "name",
                dataSource: companycontactsdatasource,

            });
        }


        function allcontactsAutoCompleteEditor(container, options) {
            leadContactId = 0;

            $('<input data-text-field="contactname"  data-value-field="id"   data-bind="value:leadcontactname" style="width:245px;"/>')
                .appendTo(container)
                .kendoAutoComplete({
                    headerTemplate: '<div class="AutoCompleteHeader">' +
               '<span style="width:50%;"> Contact info</span>' +
                '<span> | </span>' +
               '<span style="width:50%;"> Company </span>' +
           '</div>',
                    template: '<span class="k-state-default" style="width:50%;">#:data.contactname #</span>' +
                '<span> | </span>' +
                      '<span class="k-state-default" style="width:50%;"> #:data.cpName #</span> ',
                    dataValueField: "id",
                    dataTextField: "contactname",
                    suggest: true,
                    placeHolder: "Enter Caller Name here",
                    filter: "contains",
                    index: 1,
                    height: 400,
                    minLength: 1,
                    dataSource: allcontactsdatasource,
                    select: function (e) {
                        var dataItem = this.dataItem(e.item.index());
                        leadContactId = dataItem.id;
                    }
                });


        }


        function oportunityDropdownEditor(container, options) {
            $('<input id="timeZoneId" data-text-field="dataText" data-value-field="dataText"  data-bind="value:' + options.field + '"/>').appendTo(container).kendoDropDownList({
                autoBind: true,
                dataSource: {
                    data: [{ dataText: "0%", dataValue: "1" },
                        { dataText: "10%", dataValue: "2" },
                        { dataText: "20%", dataValue: "3" },
                        { dataText: "30%", dataValue: "4" },
                     { dataText: "40%", dataValue: "5" },
                        { dataText: "50%", dataValue: "6" },
                        { dataText: "60%", dataValue: "7" },
                    { dataText: "70%", dataValue: "8" },
                    { dataText: "80%", dataValue: "9" },
                   { dataText: "90%", dataValue: "10" },
    { dataText: "100%", dataValue: "11" }]
                }

            });
        }

        

      

   

</script>

